package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.Linkman;
import com.bwhx.eruler.service.BuyerService;
import com.bwhx.eruler.util.Constant;
import com.bwhx.eruler.util.StringUtils;
/**
 * E尺购买者 一般指的是公司
 * @author Cuilitang
 *
 */
@Controller
public class BuyerController extends BaseController {
	
	@Autowired
	BuyerService buyerService;
	
	@ResponseBody
	@RequestMapping(value = "/page.buyerList.ashx")
	 public Map<String,Object> buyerList(Integer page,Integer rows,Buyer buyer, @RequestBody String b) {
		 log.info(b);
		 if(this.getSessionStaff().getsDeptId().equals(13)){
			 buyer.setCreatorId(this.getSessionStaff().getsId()); 
		 }
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,buyer);
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			List<Buyer> list = buyerService.list(p);
			Integer count = buyerService.count(buyer);
			log.info(list);
			log.info(count);
			
			map.put("total",count);//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			for (Buyer buyer1 : list) {
				log.info(buyer1);
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.buyerListAll.ashx")
	 public List<Buyer> buyerListAll(Integer creatorId) {
		
		
		List<Buyer> list = new ArrayList<Buyer>();
		try{
			Staff s = this.getSessionStaff();
			Buyer buyer = new Buyer();
			if(s.getsDeptId().equals(13)){
				buyer.setCreatorId(s.getsId());
			}else{
				buyer.setCreatorId(creatorId);
			}
			Page page = new Page();
			page.setSearchObj(buyer);
			list = buyerService.list(page);

		}catch(Exception e){
			log.error(getTrace(e));
				}
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateBuyer.ashx")
	public HashMap<String,Object> saveOrUpdateBuyer(Buyer buyer) {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		try{
			
			Staff sessionUser = this.getSessionStaff();
			buyer.setCreatorId( sessionUser.getsId());
			buyer.setCreatorName( sessionUser.getsName());
			
			if(buyer.getbId()==-1){
				
				if(!StringUtils.isEmpty(buyer.getbName())&&!StringUtils.isEmpty(buyer.getbCode())&&buyer.getbCode().matches("[0-9A-Z]{8}")){
					
					Buyer b = buyerService.findByBNameAndBCode(buyer);
					
	 				if(b!=null&&(buyer.getbCode().equals(b.getbCode())||buyer.getbName().equals(b.getbName()))){
	 					 resultMap.put("status", "5");
	 					 resultMap.put("msg", "客户名称或者客户代码重复！");
	 					 resultMap.put("data", b);
 						
					}else{
						buyer.setbId(null);
						buyer.setbInsertTime(System.currentTimeMillis()/1000);
						buyerService.insert(buyer);
						resultMap.put("status", "1");
						resultMap.put("msg", "操作成功！");
					}
				}else{
					resultMap.put("status", "2");
					resultMap.put("msg", "失败！<br><br>1.客户名称或者客户代码为空<br><br>2.客户代码格式错误<包含1-9A-Z的8位字符串>！");
  				}
	
			}else{
				
				Buyer resultBuyer = buyerService.get(buyer.getbId());
				
				if(resultBuyer==null){
					resultMap.put("status", "3");
					resultMap.put("msg", "未找到您修改的客户，请刷新当前页面！");
 				}else{
 					if(StringUtils.isEmpty(resultBuyer.getbCode())||resultBuyer.getbCode().equals(buyer.getbCode())){
 						buyerService.update(buyer);
 						resultMap.put("status", "1");
						resultMap.put("msg", "操作成功！");
 					}else {
						resultMap.put("status", "4");
						resultMap.put("msg", "客户代码不允许修改！");
 					}
 					
				}
				

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("status", "0");
			resultMap.put("msg", "失败！");
 			return resultMap;
		}
		return resultMap;
		
		
 	  }
	
	
	@RequestMapping(value = "/page.buyerdetails.ashx")
	 public ModelAndView BuyerDetails(@RequestParam String bId){
		Buyer buyer = buyerService.get(bId);
		ModelAndView modelAndView = new ModelAndView("/user/buyerdetails.jsp","buyer",buyer);
		
		return modelAndView;
	  }
	
	
	@ResponseBody
	@RequestMapping("/csharpclient.listBuyer.ashx")
	 
	 public Map<String,Object> inputRuler(@RequestBody Buyer buyer) {
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		if(StringUtils.isEmpty(buyer.getbName())){
			resultMap.put("msg", "名称关键词参数为空！");
			resultMap.put("status", "0");
			return resultMap;
		}
 	
 		try{
 			
 			List<Buyer> buyers = buyerService.findByName(buyer.getbName());
 			resultMap.put("data", buyers);
 			resultMap.put("msg", "获取成功");
 			resultMap.put("status", "1");
 			
		
		}catch(Exception e){
			log.error(getTrace(e));
 			resultMap.put("msg", "获取失败！");
			resultMap.put("status", "0");
		}
		
		
		
	    return resultMap;  
	  }
	
	
	



}
